package com.module.manager;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 自定义缓存管理器，用于使用jcache 和 redis 缓存。
 * @author chengluqiang
 * @since 2019-02-27 21:47
 */
@Data
@Slf4j
public class CustomCacheManager implements CacheManager {

    private CacheManager redisCacheManager;
    private CacheManager jCacheCacheManager;
    private final static String redisPrefix = "redis";

    @Override
    public Cache getCache(String name) {
        log.info("name======"+name);
        if (name.startsWith(redisPrefix))  {
        	log.info("CustomCacheManager======"+redisCacheManager);
        	return redisCacheManager.getCache(name);
        } else {
        	log.info("CustomCacheManager======"+jCacheCacheManager);
        	return jCacheCacheManager.getCache(name);
        }
    }

    @Override
    public Collection<String> getCacheNames() {
        Collection<String> cacheNames = new ArrayList<String>();
        if (redisCacheManager != null) {
            cacheNames.addAll(redisCacheManager.getCacheNames());
        }
        if (jCacheCacheManager != null) {
            cacheNames.addAll(jCacheCacheManager.getCacheNames());
        }
        return cacheNames;
    }

}
